const PipeGroup = require('PipeGroup');
cc.Class({
    extends: cc.Component,

    properties: {
       pipePrefab:cc.Prefab,
       pipeMoveSpeed:-300,
       pipeSpacing:400
    },
    

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.pipeList =[];
        this.isRunning = false;
    },
    startSpawn(){
        this.spawnPipe();
        let spawnInterval = Math.abs(this.pipeSpacing/this.pipeMoveSpeed);
        this.schedule(function(){
            this.spawnPipe()
        },spawnInterval)
        this.isRunning=true;
        
    },
    spawnPipe(){
        let pipeGroup = null;
        if(cc.pool.hasObject(PipeGroup)){
            pipeGroup = cc.pool.getFromPool(PipeGroup);
        }else{
            pipeGroup= cc.instantiate(this.pipePrefab).getComponent(PipeGroup);
        }
        this.node.addChild(pipeGroup.node)
        pipeGroup.node.active=true;
        pipeGroup.init(this);
        this.pipeList.push(pipeGroup);
    
    },
    recyclePipe(pipe){
        pipe.node.removeFromParent();
        pipe.node.active=false;
        cc.pool.putInPool(pipe);
        
    },
    getNext(){
        return this.pipeList.shift();
    },
    reset(){
        this.unschedule(this.spawnPipe);
        this.pipeList=[];
        this.isRunning = false;
    },

    start () {

    },

    // update (dt) {},
});
